package controller.action;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import model.domain.WeatherBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class DataParser {
	public static void parsingData() {
		ArrayList<WeatherBean> alldata = null;
		HashMap<String, Object> map = new HashMap<String, Object>();
		Document doc;
		try {
			doc = Jsoup.connect("http://m.naver.com").get();
			Elements reff = doc.select("li.toif2_l");
			Elements url = doc.getElementsByClass("toif2_aw");
			Elements location = doc.getElementsByClass("toif2_h");
			Elements degree = doc.getElementsByClass("toif2_deg");
			Elements mi = doc.getElementsByClass("mi");
			Elements state = doc.getElementsByClass("imwt");
			alldata = new ArrayList<WeatherBean>();
			for (int i = 5; i < reff.size(); i++) {
				alldata.add(i - 5, new WeatherBean(url.get(i).attr("href"),
						location.get(i).text(), degree.get(i - 5).text(), mi
								.get(i - 5).text(), state.get(i - 5).text()));
			}
			JSONArray jsonarray = JSONArray.fromObject(alldata);
			map.put("dailyList", jsonarray);
			JSONObject jsonObject = JSONObject.fromObject(map);
			FileWriter out = new FileWriter(
					"C:\\Users\\오션45\\Desktop\\Naver_UX\\WebContent\\weather.json");
			System.out.println("json-" + jsonObject);
			out.write(jsonObject.toString());
			out.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void realtimeData() {
		Document doc;
		try {
			doc = Jsoup.connect("http://m.naver.com").get();
			String[] list = doc.getElementById("recommend_json").toString().split("\n");
			FileWriter out = new FileWriter(
					"C:\\Users\\오션45\\Desktop\\Naver_UX\\WebContent\\realtime.json");
			for (String result : list) {
				if (result.contains("realtimeKeywordList")) {
					// System.out.println(result.split("=")[1]);
					// System.out.println(result.split("=")[1].split("} catch")[0]);
					out.write(result.split("=")[1].split("; } catch")[0]);
				}
				/*
				 * if(result.contains("try")){ System.out.println(result); }
				 */
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
